package com.macaline.controller.user;

import com.macaline.Golbal;
import com.macaline.bean.Result;
import com.macaline.controller.base.BaseController;
import com.macaline.entity.User;
import com.macaline.service.base.BaseService;
import com.macaline.service.impl.user.UserServiceimpl;
import com.macaline.service.perssion.RoleService;
import com.macaline.service.user.AdminUserService;
import com.macaline.util.DateUtil;
import com.macaline.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Api(tags = "用户基本操作接口")
@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User> {
	
	@Resource
	UserServiceimpl userService;

	@Resource
	AdminUserService adminUserService;
	
	@Resource
	RoleService roleService;

	@Override
	public BaseService<User> getService() {
		return userService;
	}
	
	
	@ResponseBody
	@RequestMapping("/getUserById")
	public Object getUserById(@PathVariable("id") Integer id) {
		User user = userService.getUser(id);
		return user;
	}
	

	@ResponseBody
	@RequiresPermissions("addUser")
	@ApiOperation(value = "注册新用户-warm_joy", nickname = "warm_joy")
	@RequestMapping(value = "/regist", method = {RequestMethod.POST})
	public Result registUser(User user, HttpServletRequest request) {
		//为用户设置一个随机的密码加密盐值
		user.setSalt(RandomUtil.getMD5RandomSalt());
		//使用盐值对用户的密码进行MD5加密
		String password = new Md5Hash(user.getPassWord(),user.getSalt() , Golbal.USER_PASSWORD_MD5_HASHITERATIONS).toString();
		//将加密后的密码赋给用户
		user.setPassWord(password);
		//添加用户创建时间
		user.setRegTime(DateUtil.getTime4Linux10());
		userService.save(user);
		return Result.success();
	}

	@RequestMapping("/registPage")
	public String registPage() {

		return "addUser";
	}
	
	@ResponseBody
	@RequestMapping("/addUser")
	public User addUser(User user) {
		return adminUserService.addUser(user);
	}
	
	@ResponseBody
	@RequestMapping("getUser")
	public User getUser(Integer id) {
		return userService.getUser(id);
	}


}
